import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.io.*;

public class Driver {
	
	public void run(){
		//String nameOS = "os.name";  
		//System.out.println(System.getProperty(nameOS));
		
		FileHandler input = new FileHandler("input.txt"); 
		String inputArray[] = input.readFile();
		String [] fileList;
		ArrayList<Token> totals = new ArrayList<Token>();
		
		System.out.println("TSM_Scanner Output");
		System.out.println("==================");
		for(String path : inputArray ){
			
			path = path.replaceAll("\\n", "");
			path = path.replaceAll("\\r", "");
	
			Finder finder = new Finder(path,".lua");
			finder.runVisit();
			fileList = finder.getFiles();
			//finder.printList();
			
			
			System.out.println("\n"+path);
			for (int i =0; i < path.length(); i++)
				System.out.print("-");
			System.out.println("\n");
			
			
			
			for(String file : fileList ){
				
				Token newToken;
				ArrayList<Token> list = new ArrayList<Token>();
				
				Scanner scanner = new Scanner(file);
				while (scanner.isEOF()){
					newToken = scanner.dispatcher();
					if (newToken != null){
						int index = searchList(newToken, list);
						if (index < 0)
							list.add(newToken);
						else
							list.get(index).addLineNum(newToken.getLine());
						
						index = searchList(newToken, totals);
						
						if (index < 0)
							totals.add(newToken);
						else
							totals.get(index).incrementCount();
						
					}//end if
				}//end while
				

				if(!list.isEmpty()){
					System.out.println(file);
					for (int i = 0; i <= list.size()-1; i++){
						System.out.println(list.get(i).getToken() + " Lines: "+list.get(i).getLineAsString());
					}//end for
					System.out.println();	
				}//end if
						
			}//end inner for

		}//end outer for
		

		
		Collections.sort(totals);
		
		System.out.println("Totals");
		System.out.println("======");
		if(!totals.isEmpty()){
			for (int i = 0; i <= totals.size()-1; i++){
				System.out.println(totals.get(i).getToken() + " Count: "+totals.get(i).getCount());
			}//end for
			System.out.println();	
		}//end if
		
	}//end run

	private  int searchList(Token token, ArrayList<Token> list){
		for (int i = 0; i <= list.size()-1; i++){
			if (token.CompareTo(list.get(i)) == 1)
				return i;
		}//end for
		return -1;
	}//end searchList
	

	 
}//end class
